package com.controller;


import com.entity.dto.Demo;
import com.entity.dto.Demo1;
import com.entity.dto.Demo2;
import com.handler.BizException;
import com.util.Response;
import com.util.pdfUtil.WordUtil;
import excel.CarExcelReq;
import excel.MathematicsTopicDto;
import excel.file.FileUtil;
import excel.poi.ExcelUtil;
import excel.poi.ExcelUtil2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author
 * @ClassName: Controller
 * @Description: 模板工具类
 * @date 2021/4/23 17:23
 * @Version 1.0
 */
@RestController
@RequestMapping("/template")
@Api(tags = "模板工具类")
public class TemplateController {

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    @ApiOperation(value = "下载模板")
    public void importTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ExcelUtil<CarExcelReq> util = new ExcelUtil<>(CarExcelReq.class);
        String filename = util.importTemplateExcel("车辆保养记录");
        FileUtil.encodeByAgent(request, filename);
        FileUtil.downloadFileOnce(filename, response);

//        return Response.createSuccess("成功");
    }


    /**
     * 导出
     *
     * @param carExcelReq
     * @return
     */
    @PostMapping("/exportMaintain")
    @ApiOperation(value = "导出数据自动合并单元格")
//    @DataPermission(permissionId = "")
    public void exportMaintain(@RequestBody CarExcelReq carExcelReq, HttpServletResponse response) {
        // 查询结果
        List<CarExcelReq> list = new ArrayList<>();
        for (int q = 0; q < 2; q++) {
            CarExcelReq req = new CarExcelReq();
            req.setCarNo("车牌" + q);
            req.setVin("车架号" + q);
            req.setEngineNo("aaa" + q);
            List<Demo> demoList = new ArrayList<>();
            for (int i = 0; i < 2; i++) {
                Demo demo = new Demo();
                demo.setName("一层Name" + i);
                demo.setAge("一层年龄" + i);
                Demo1 demo1 = new Demo1();
                demo1.setName1("对象名称" + i);
                demo1.setAge1("对象年龄" + i);
                demo.setDemo1(demo1);

                List<Demo2> demo2s = new ArrayList<>();
                for (int j = 0; j < 2; j++) {
                    Demo2 demo2 = new Demo2();
                    demo2.setName2("二层名称" + j);
                    demo2.setAge2("二层年龄" + j);
                    demo2s.add(demo2);
                }
                demo.setDemo2List(demo2s);
                demoList.add(demo);
            }
            req.setDemoList(demoList);
            list.add(req);
        }

        // 业务数据处理

        // 导出合并单元格定制化工具
        ExcelUtil2<CarExcelReq> excelUtil = new ExcelUtil2(CarExcelReq.class);
        String export = excelUtil.exportExcel(list, "车辆保养记录");

        try {
            FileUtil.downloadFileOnce(export, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("文件输出失败");
        }
    }

    @PostMapping("/exportMaintain2")
    @ApiOperation(value = "导出数据小学算术")
//    @DataPermission(permissionId = "")
    public void exportMaintain2(@RequestBody CarExcelReq carExcelReq, HttpServletResponse response) {

        // 业务数据处理

        // 数学算式生成
        List<MathematicsTopicDto> list = new ArrayList<>();
        MathematicsTopicDto.getEquation(list, "乘除法", 9, 18);

        // 导出
        ExcelUtil<MathematicsTopicDto> excelUtil = new ExcelUtil(MathematicsTopicDto.class);
        String export = excelUtil.exportExcel(list, "小学算术");

        try {
            FileUtil.downloadFileOnce(export, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("文件输出失败");
        }
    }



    /**
     * 导入
     */
    @PostMapping("/import")
    @ApiOperation(value = "导入模板")
    public Response importData(@RequestParam(value = "file") MultipartFile file) {

        //获取上传的文件名包括后缀
        String fileNameTemp = file.getOriginalFilename();

        //判断文件是否是excel文件
        if (!fileNameTemp.endsWith("xls") && !fileNameTemp.endsWith("xlsx")) {
            throw new RuntimeException("导入文件格式不对，请导入正确格式的文件");
        }

        //excel 解析
        ExcelUtil excelUtil = new ExcelUtil(CarExcelReq.class);
        List<CarExcelReq> excelReqList = null;

        try {
            excelReqList = excelUtil.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("文件读取失败");
        }
        if (excelReqList.size() == 0) {
            return Response.createError("请录入数据");
        }
        if (excelReqList.size() > 5000) {
            return Response.createError("一次只能录入数据5000条");
        }

        //批量导入 业务处理
        return Response.createSuccess();
    }


    /**
     * word下载
     */
    @PostMapping("/importWord")
    @ApiOperation(value = "word表格下载")
    public void importWord(HttpServletRequest request, HttpServletResponse response) throws Exception {
        WordUtil wordUtil = new WordUtil();
        Map params = new HashMap();
        params.put("${position}", "java开发");
        params.put("${name}", "段然涛");
        params.put("${sex}", "男");
        params.put("${national}", "汉族");
        params.put("${birthday}", "生日");
        params.put("${address}", "许昌");
        params.put("${height}", "165cm");
        params.put("${biYeDate}", "1994-02-03");
        params.put("${landscape}", "团员");
        params.put("${zhuanYe}", "社会工作");
        params.put("${xueLi}", "本科");
        params.put("${school}", "江西科技师范大学");
        params.put("${chengji}", "100分ヾ(o◕∀◕)ﾉヾ");
        params.put("${phone}", "177");
        params.put("${eMail}", "157");
        params.put("${remake}", "我三大块电话卡号地块很卡对话框辣椒我我而后卫和哦我王洪瑞宏伟还叫我然后额环绕味儿开完会让客户我搜房海上繁花搜发洪水佛山佛山佛还哦");
        try {
            Map<String, Object> header = new HashMap<String, Object>();
            header.put("width", 100);
            header.put("height", 150);
            header.put("type", "jpg");
//            header.put("content", WordUtils.inputStream2ByteArray(new FileInputStream("C:/Users/Administrator/Desktop/jar包/11.jpg"), true));
            params.put("${header}", header);
            Map<String, Object> header2 = new HashMap<String, Object>();
            header2.put("width", 100);
            header2.put("height", 150);
            header2.put("type", "jpg");
//            header2.put("content", WordUtils.inputStream2ByteArray(new FileInputStream("C:/Users/Administrator/Desktop/jar包/22.jpg"), true));
            params.put("${header2}", header2);
            List<List<String[]>> list = new ArrayList<List<String[]>>();
            List<String[]> testList = new ArrayList<String[]>();//一个需要插入的表是一个集合
            testList.add(new String[]{"1", "1AA", "1BB", "1CC"});
            testList.add(new String[]{"2", "2AA", "2BB", "2CC"});
            testList.add(new String[]{"3", "3AA", "3BB", "3CC"});
            testList.add(new String[]{"4", "4AA", "4BB", "4CC"});
            list.add(testList);
            // 读取文件
            File file = new File("个人信息表.docx");
            String path = file.getPath();
//            String path="myUtils/个人信息表.docx";  //模板文件位置
            String fileName = new String("测试文档.docx".getBytes("UTF-8"), "UTF-8");    //生成word文件的文件名
            wordUtil.getWord(path, params, list, fileName, "", response);


            // 导出word文档多个复选框勾选,需求依赖
                   /* <dependency>
                        <groupId>com.deepoove</groupId>
                        <artifactId>poi-tl</artifactId>
                        <version>1.8.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-test</artifactId>
                    </dependency>*/
//            params.put("${remake}", new TextRenderData("R",new Style("Wingdings 2",14)));
//            params.put("${remake}", new TextRenderData("\u25A1",new Style("Wingdings 2",14)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
